package top.blackr.folivora.common.core.exception;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import lombok.SneakyThrows;
import top.blackr.folivora.common.core.enums.ResultEnum;

import java.io.Serial;

/**
 * 异常格式化
 */
public class BizExceptionSerializer extends StdSerializer<BizException> {

	@Serial
	private static final long serialVersionUID = -4773290759177324295L;

	public BizExceptionSerializer() {
		super(BizException.class);
	}

	@Override
	@SneakyThrows
	public void serialize(BizException value, JsonGenerator gen, SerializerProvider provider) {
		gen.writeStartObject();
		gen.writeObjectField("code", ResultEnum.FAIL.getCode());
		gen.writeStringField("msg", value.getMessage());
		gen.writeStringField("data", ResultEnum.FAIL.getCnMes());
		gen.writeEndObject();
	}

}
